package com.henke.convertruck;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	public int ItemSelectedFrom;
	public int ItemSelectedTo;
	
	public String stringFrom;
	public String stringTo;
	
	private EditText textInfo;
	private TextView textHistory;
	
	private float outputResult;

	public void setItemSelectedFrom(int itemSelectedFrom) {
		ItemSelectedFrom = itemSelectedFrom;
	}

	public void setItemSelectedTo(int itemSelectedTo) {
		ItemSelectedTo = itemSelectedTo;
	}

	public void setStringFrom(String stringFrom) {
		this.stringFrom = stringFrom;
	}

	public void setStringTo(String stringTo) {
		this.stringTo = stringTo;
	}

	public void setTextInfo(EditText textInfo) {
		this.textInfo = textInfo;
	}

	public void setTextHistory(TextView textHistory) {
		this.textHistory = textHistory;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//
		textInfo = (EditText) findViewById(R.id.editText1);
		textHistory = (TextView) findViewById(R.id.textViewHistory);
		
		
		Spinner spinnerFrom = (Spinner) findViewById(R.id.spinnerFrom);
		Spinner spinnerTo	= (Spinner) findViewById(R.id.spinnerTo);
		
		// Create an ArrayAdapter using the string array and a default spinner layout
		final ArrayAdapter<CharSequence> adapterFrom = ArrayAdapter.createFromResource(this,
		        R.array.ListFrom, android.R.layout.simple_spinner_item);
		final ArrayAdapter<CharSequence> adapterTo = ArrayAdapter.createFromResource(this,
		        R.array.ListTo, android.R.layout.simple_spinner_item);
		
		// Specify the layout to use when the list of choices appears
		adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		// Apply the adapter to the spinner
		spinnerFrom.setAdapter(adapterFrom);
		spinnerTo.setAdapter(adapterTo);
		
		// create a Title
		spinnerFrom.setPrompt("From");
		spinnerTo.setPrompt("To");
		
		spinnerFrom.setSelection(0);
		spinnerTo.setSelection(0);
		
		// set click handler for spinnerFrom
		spinnerFrom.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
				//Toast.makeText(getBaseContext(), "Convert from " + adapterFrom.getItem(position), Toast.LENGTH_SHORT).show();
				setItemSelectedFrom(position);
				setStringFrom((String) adapterFrom.getItem(position));
			}
			@Override
		    public void onNothingSelected(AdapterView<?> arg0) {
		    }
		});
		
		// set click handler for spinnerTo
		spinnerTo.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
				//Toast.makeText(getBaseContext(), "To " + adapterTo.getItem(position), Toast.LENGTH_SHORT).show();
				setItemSelectedTo(position); 
				setStringTo((String) adapterTo.getItem(position));
			}
			@Override
		    public void onNothingSelected(AdapterView<?> arg0) {
		    }
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	/*---------------------------------------------------------------------------------*/

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		TextView tvInfo = (TextView) findViewById(R.id.textViewHistory);

		// Operations for the selected menu item
		switch (item.getItemId()) {
		case R.id.menu_length:
			//tvInfo.setText("\nYou have chosen a Length!\n\n");
			Toast.makeText(this, "You have chosen a Length Converter", Toast.LENGTH_LONG).show();
			return true;
		case R.id.menu_volume:
			//tvInfo.setText("\nYou have chosen a Volume!\n\n");
			Toast.makeText(this, "You have chosen a Volume Converter", Toast.LENGTH_LONG).show();
			Intent volIntent = new Intent(MainActivity.this, VolumeActivity.class);
			startActivity(volIntent);
			return true;
		case R.id.menu_temperature:
			//tvInfo.setText("\nYou have chosen a Temperature!\n\n");
			Toast.makeText(this, "You have chosen a Temperature Converter", Toast.LENGTH_LONG).show();
			Intent tempIntent = new Intent(MainActivity.this, TemperatureActivity.class);
			startActivity(tempIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	

	/***********************************************************************************/
	
	public void buttonConvert_Click(View view){
    	String vv_text = "";
    	
		switch(view.getId()){
    	case R.id.buttonConvert:
    		
    		if ( textInfo.getText().length() == 0 ){
    			Toast.makeText(this, "Please enter a input value", Toast.LENGTH_LONG).show();
    			return;
    		}
    		
    		Float inputValue = Float.parseFloat(textInfo.getText().toString());
    		
    		// do convert
    		outputResult = convertValue(inputValue, ItemSelectedFrom, ItemSelectedTo);
    		textInfo.setText(String.valueOf(outputResult));
    		
    		vv_text = inputValue+" ("+stringFrom+") = "+outputResult+" ("+stringTo+")\n";
    		textHistory.append(String.valueOf(vv_text));
    	}
    }

	//case R.id.button_convert:	
	/**
	 * Function of Convert value. 
	 * */
	private float convertValue(float i_inputValue, int i_From, int i_To) {
		// TODO Auto-generated method stub
		switch (i_From){
			case 0:{ // from KM
				switch (i_To){
					case 0:{ // to Kilometers
						return i_inputValue;						
					}
					case 1:{ // to Meter
						return (float)(i_inputValue*1000);
					}
					case 2:{ // to Mile
						return (float)(i_inputValue * 0.62);
					}
					case 3:{ // to Yard
						return (float)((i_inputValue*1000) * 1.0936133);
					}
					case 4:{// to Foot
						return (float)((i_inputValue*1000) * 3.2808399);
					}
					case 5:{// to Inch
						return (float)((i_inputValue*100000) * 0.393700787);
					}
					default:{
						Toast.makeText(this, "Unrecognised error by converting from (Km)"
								, Toast.LENGTH_LONG).show();
						break;
					}
				}
				break;
			}
			case 1:{ // from Meter
				switch (i_To){
					case 0:{ // to kilometers
						return (float)(i_inputValue * 0.001);						
					}
					case 1:{ // to Meter
						return (float)(i_inputValue);
					}
					case 2:{ // to Mile
						return (float)(i_inputValue * 0.000621371192);
					}
					case 3:{ // Yard
						return (float)(i_inputValue * 1.0936133);
					}
					case 4:{// to Foot
						return (float)(i_inputValue * 3.2808399);
					}
					case 5:{// to Inch
						return (float)(i_inputValue * 39.3700787);
					}
					default:{
						Toast.makeText(this, "Unrecognised error by converting from (Mi)"
								, Toast.LENGTH_LONG).show();
						break;
					}
				}
				break;
			}
			case 2:{ // from MI
				switch (i_To){
					case 0:{ // to kilometers
						return (float)(i_inputValue * 1.609344);						
					}
					case 1:{ // to Meter
						return (float)(i_inputValue * 1609.344);
					}
					case 2:{ // to Mile
						return (float)(i_inputValue);
					}
					case 3:{ // Yard
						return (float)(i_inputValue * 1760);
					}
					case 4:{// to Foot
						return (float)(i_inputValue * 5280);
					}
					case 5:{// to Inch
						return (float)(i_inputValue * 63360);
					}
					default:{
						Toast.makeText(this, "Unrecognised error by converting from (Mi)"
								, Toast.LENGTH_LONG).show();
						break;
					}
				}
				break;
			}
			case 3:{ // from YD
				switch (i_To){
					case 0:{ // to kilometers
						return (float)(i_inputValue * 0.0009144);						
					}
					case 1:{ // to Meter
						return (float)(i_inputValue * 0.9144);
					}
					case 2:{ // to Mile
						return (float)(i_inputValue * 0.000568181818);
					}
					case 3:{ // Yard
						return (float)(i_inputValue);
					}
					case 4:{// to Foot
						return (float)(i_inputValue * 3);
					}
					case 5:{// to Inch
						return (float)(i_inputValue * 36);
					}
					default:{
						Toast.makeText(this, "Unrecognised error by converting from (Mi)"
								, Toast.LENGTH_LONG).show();
						break;
					}
				}
				break;
			}
			case 4:{ // from FT
				switch (i_To){
					case 0:{ // to kilometers
						return (float)(i_inputValue * 0.0003048);						
					}
					case 1:{ // to Meter
						return (float)(i_inputValue * 0.3048);
					}
					case 2:{ // to Mile
						return (float)(i_inputValue * 0.000189393939);
					}
					case 3:{ // Yard
						return (float)(i_inputValue * 0.333333333);
					}
					case 4:{// to Foot
						return (float)(i_inputValue);
					}
					case 5:{// to Inch
						return (float)(i_inputValue * 12);
					}
					default:{
						Toast.makeText(this, "Unrecognised error by converting from (Mi)"
								, Toast.LENGTH_LONG).show();
						break;
					}
				}
				break;
			}
			case 5:{ // from IN
				switch (i_To){
					case 0:{ // to kilometers
						return (float)(i_inputValue * 0.0000254);						
					}
					case 1:{ // to Meter
						return (float)(i_inputValue * 0.0254);
					}
					case 2:{ // to Mile
						return (float)(i_inputValue * 0.000015782828300000002);
					}
					case 3:{ // Yard
						return (float)(i_inputValue * 0.0277777778);
					}
					case 4:{// to Foot
						return (float)(i_inputValue * 0.0833333333);
					}
					case 5:{// to Inch
						return (float)(i_inputValue);
					}
					default:{
						Toast.makeText(this, "Unrecognised error by converting from (Mi)"
								, Toast.LENGTH_LONG).show();
						break;
					}
				}
				break;
			}
			default:{
				Toast.makeText(this, "Unrecognised error", Toast.LENGTH_LONG).show();
				break;
			}
		}
		return 0;
	}

}
